import pyautogui
import keyboard
from pynput import mouse

def track_mouse_clicks():
    """连续获取鼠标点击位置，直到按下Enter键"""
    print("开始记录鼠标点击位置，按Enter键结束...")
    
    click_positions = []
    listening = True
    
    # 鼠标点击事件处理函数
    def on_click(x, y, button, pressed):
        nonlocal listening
        if not listening:
            return False  # 停止监听
        if button == mouse.Button.left and pressed:
            click_positions.append((x, y))
            print(f"点击位置: X={x}, Y={y}")
    
    # 启动鼠标监听器
    mouse_listener = mouse.Listener(on_click=on_click)
    mouse_listener.start()
    
    try:
        # 等待Enter键被按下
        keyboard.wait('ctrl')
        print("\n已检测到Enter键，停止记录")
    except KeyboardInterrupt:
        print("\n程序被用户中断")
    finally:
        # 停止监听
        listening = False
        mouse_listener.stop()
        mouse_listener.join()
    
    return click_positions

if __name__ == "__main__":
    positions = track_mouse_clicks()
    
    # 打印所有记录的点击位置
    if positions:
        print("\n所有点击位置汇总:")
        for i, (x, y) in enumerate(positions, 1):
            print(f"点击 #{i}: X={x}, Y={y}")
    else:
        print("\n没有记录到任何点击位置")    